/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.service;

import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.service.CrudService;
import com.jiadakeji.ppm.common.utils.DateUtils;
import com.jiadakeji.ppm.modules.business.dao.FeeMonthDao;
import com.jiadakeji.ppm.modules.business.entity.DTO.MonthInitDTO;
import com.jiadakeji.ppm.modules.business.entity.FeeMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 缴费月份Service
 * @author HHAOYU
 * @version 2019-07-09
 */
@Service
@Transactional(readOnly = true)
public class FeeMonthService extends CrudService<FeeMonthDao, FeeMonth> {
	@Autowired
	private FeeChargeService feeChargeService;

	@Override
	public FeeMonth get(String id) {
		return super.get(id);
	}

	@Override
	public List<FeeMonth> findList(FeeMonth feeMonth) {
		return super.findList(feeMonth);
	}

	@Override
	public Page<FeeMonth> findPage(Page<FeeMonth> page, FeeMonth feeMonth) {
		return super.findPage(page, feeMonth);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(FeeMonth feeMonth) {
		super.save(feeMonth);
	}


	@Override
	@Transactional(readOnly = false)
	public void delete(FeeMonth feeMonth) {
		//TODO 删除其它关联表
		
		super.delete(feeMonth);
	}


	/**
	 *   获取当前的月份
	 * @return
	 */
	public MonthInitDTO preMontinit(){

		FeeMonth feeMonth = new FeeMonth();
		MonthInitDTO monthInitDTO = dao.selectMonthInit(feeMonth);
		if(monthInitDTO != null){
			getInitMonthHaveMonth(monthInitDTO);
		}else{
			monthInitDTO = new MonthInitDTO();
			getInitMonthFirstInit(monthInitDTO);
		}
		return monthInitDTO;
	}

	/**
	 *
	 * 初始化--月份，第一次初始化
	 * @autohor xuelongjiang
	 *
	 */

	private   MonthInitDTO getInitMonthFirstInit(MonthInitDTO monthInit){



		LocalDate monthInitMonth = LocalDate.now();
		LocalDate firstDatOfMonth = monthInitMonth.with(TemporalAdjusters.firstDayOfMonth());
		LocalDate lastDateOfMonth  = monthInitMonth.with(TemporalAdjusters.lastDayOfMonth());

		monthInit.setInitCurrentMonth(DateUtils.tranLocalDateToDate(monthInitMonth));
		monthInit.setInitStartDay(DateUtils.tranLocalDateToDate(firstDatOfMonth));
		monthInit.setInitEndDay(DateUtils.tranLocalDateToDate(lastDateOfMonth));

		return monthInit;

	}

	/**
	 *
	 * 初始化- 月份  有之前的月份
	 * @author xueolongjiang
	 *
	 */
	private   MonthInitDTO getInitMonthHaveMonth(MonthInitDTO monthInit){

		LocalDate nextMonth = DateUtils.tranDateToLocalDate(monthInit.getCurrentMonth()).with(TemporalAdjusters.firstDayOfNextMonth());
		LocalDate  lastMonthDay = nextMonth.with(TemporalAdjusters.lastDayOfMonth());

		monthInit.setInitCurrentMonth(DateUtils.tranLocalDateToDate(nextMonth));
		monthInit.setInitStartDay(DateUtils.tranLocalDateToDate(nextMonth));
		monthInit.setInitEndDay(DateUtils.tranLocalDateToDate(lastMonthDay));

		return monthInit;

	}


	/**
	 *  验证初始化的月份是否符合要求<br>
	 *  月份不能已经存在<br>
	 *
	 * @param monthInit
	 * @return
	 */
	public boolean validMonthInit(MonthInitDTO monthInit){

		FeeMonth feeMonth = new FeeMonth();
		feeMonth.setCurrentMonth(monthInit.getInitCurrentMonth());
		List<FeeMonth> feeMonthList = dao.selectFeeMonthByCurrentMonth(feeMonth);
		if(!feeMonthList.isEmpty()){
			return false;
		}
		return true;
	}


	/**
	 * 保存新的 月份
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public FeeMonth saveMonthInit(MonthInitDTO monthInit){

		FeeMonth preFeeMonth = new FeeMonth();
		FeeMonth currentFeemonth = new FeeMonth();

		preFeeMonth.setId(monthInit.getCurrentId());
		preFeeMonth.setIsCurrent("0");//否 设置为不是当前月
		currentFeemonth.preInsert();
		currentFeemonth.setCurrentMonth(monthInit.getInitCurrentMonth());
		currentFeemonth.setStartDate(monthInit.getInitStartDay());
		currentFeemonth.setEndDate(monthInit.getInitEndDay());
		currentFeemonth.setIsCurrent("1");//是  设置为当前月

		preFeeMonth.preUpdate();
		dao.update(preFeeMonth);
		dao.insert(currentFeemonth);
		return currentFeemonth;
	}
	/**
	 * 初始化  整个系统
	 * @param monthInit
	 */
	// TODO  初始化整个系统
	@Transactional(readOnly = false)
	public void init(MonthInitDTO monthInit){
		long beginTime = System.currentTimeMillis();
		//保存初始月份
		FeeMonth feeMonth = saveMonthInit(monthInit);

		feeChargeService.initFeecharge(feeMonth);



	}

}